<?php
/**
 * @file name : modules_model.php
 * @package   :
 * @author    : Vo Thanh Minh
 * @email     : <thanhminhld@yahoo.com - thanhminhld@gmail.com>
 * @copyright : 2011
 *
 */
if (!defined('BASEPATH')) {
    exit('No direct script access');
}

class Modules_model extends Base_Model
{
    /**
     * @function __construct
     * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     */
	public function __construct()
	{
		parent::__construct();
		$this->_tbl       = 'modules';
		$this->_primary_id= 'm_id';
	}
	/**
	 * getTotalRows
	 *
	 * @author Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
	 * @access public
	 * @param array $arrWhere
	 *
	 * @return int
	 */
    public function getTotalRows($arrWhere = array(), $field_groupby = '')
    {
	    if (count($arrWhere) > 0) {
            $this->db->where($arrWhere);
        }
        if (!empty($field_groupby) && is_string($field_groupby)) {
            if ($this->db->field_exists($field_groupby, $this->_tbl)) {
                $this->db->group_by($field_groupby);
            }
        }
		$query = $this->db->get_where($this->_tbl, $arrWhere);

		return $query->num_rows();
    }
    /**
     * getTotalFields
     *
     * @author Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
	 * @access public
	 * @param array $arrWhere
	 *
	 * @return int
     */
    public function getTotalFields($field = '*')
    {
        return $this->db->count_field_results($this->_tbl, $field);
    }
	/**
	 * @function loadList
	 *
	 * @author Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
	 * @access public
	 * @return array
	 */
	public function loadList($arrWhere = array(), $limit = 10, $offset = 0)
	{
	    $this->db->order_by('m_ordering', 'asc');
        if ($limit == 'all') {
            $query = $this->db->get_where($this->_tbl, $arrWhere);
        } else {
            $query = $this->db->get_where($this->_tbl, $arrWhere, $limit, $offset);
        }
        $arrList = array();
        if ($query->num_rows() > 0) {
            foreach($query->result() as $row) {
                $arrList[] = $row;
            }
        }
		return $arrList;
	}
	/**
	 * @function loadListIn
	 * @author 	 Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
	 * @access 	 public
	 * @param  	 string
	 * @param  	 array
	 *
	 * @return	 array
	 */
	public function loadListIn($field, $arrWhereIn = array())
	{
	    if (! is_string($field) || strlen($field) <= 0) {
	        return NULL;
	    }
	    if (count($arrWhereIn) <= 0) {
	        return NULL;
	    }
	    /**
	     * Ex)
        	$names = array('Frank', 'Todd', 'James');
			$this->db->where_in('username', $names);
		*/
	    $this->db->where_in($field, $arrWhereIn);
	    $this->db->order_by('m_ordering', 'asc');
	    $query = $this->db->get($this->_tbl);
        $arrList = array();
        if ($query->num_rows() > 0) {
            foreach($query->result() as $row) {
                $arrList[] = $row;
            }
        }
		return $arrList;
	}
    /**
     * @function loadRecord
     *
     * @author Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
     * @access public
     * @param  int $m_id
     * @return object
     */
    public function loadRecord($m_id)
    {
        $where = array($this->_primary_id  => $m_id);

        $itemObj = $this->db->get_where($this->_tbl, $where);
        $arrList = array();
        if ($itemObj->num_rows() > 0) {
            foreach($itemObj->result() as $row) {
               $arrList = $row;
            }
        }
        return $arrList;
    }
	/**
	 * @function getDropList
	 *
	 * @author   Vo Thanh Minh <thanhminhld@yahoo.com - thanhminhld@gmail.com>
	 * @access	 public
	 * @return 	 string
	 *
	 */
	public function getDropList($selected = '', $arrWhere = array())
	{
	    $strOption = '';
	    $arrList = array();
	    $arrList = $this->loadList($arrWhere, 'all');
	    $primary_id  = $this->_primary_id;
	    foreach ($arrList as $key => $genre) {
            if ($selected == $genre->$primary_id) {
                $strOption .= '<option value="' . $genre->$primary_id . '" selected>&nbsp;&nbsp;&nbsp;' . $genre->m_name . '</option>';
            } else {
                $strOption .= '<option value="' . $genre->$primary_id . '">&nbsp;&nbsp;&nbsp;' . $genre->m_name . '</option>';
            }
        }
        return $strOption;
	}
}